package com.suxii.frpclient;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.suxii.frpclient.databinding.ActivityMainBinding;
import com.suxii.frpclient.fragment.HomeFragment;
import com.suxii.frpclient.fragment.LogFragment;
import com.suxii.frpclient.fragment.SettingFragment;
import com.suxii.frpclient.message.ConfigTextMessage;
import com.suxii.frpclient.message.NavStateMessage;
import com.suxii.frpclient.service.FrpService;
import com.suxii.frpclient.utils.FileUtil;
import com.suxii.frpclient.utils.RxBus;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();

    private final Map<String,Fragment> fragmentMap = new ConcurrentHashMap<>();
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initEvent();
    }

    private void initEvent(){
        initFragmentMap();
        // 订阅 RxBus 中的数据
        Disposable disposable = RxBus.getInstance().toObservable(NavStateMessage.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(data -> {
                    if (data.isState()){
                        binding.nav.setVisibility(View.VISIBLE);
                    }else {
                        binding.nav.setVisibility(View.GONE);
                    }
                });

        binding.homeBtn.setOnClickListener(v-> loadFragment(HomeFragment.NAME));
        binding.logBtn.setOnClickListener(v-> loadFragment(LogFragment.NAME));
        binding.settingBtn.setOnClickListener(v-> loadFragment(SettingFragment.NAME));

        loadFragment(HomeFragment.NAME);
        compositeDisposable.add(disposable);
    }


    private void loadFragment(String name){
        ft = getSupportFragmentManager().beginTransaction();
        fragmentMap.forEach((n,f)->{
            if (n.equals(name)){
                ft.show(f);
            }else {
                ft.hide(f);
            }
        });
        ft.commit();
    }

    private void initFragmentMap(){
        ft = getSupportFragmentManager().beginTransaction();

        HomeFragment homeFragment = HomeFragment.newInstance();
        fragmentMap.put(HomeFragment.NAME,homeFragment);

        LogFragment logFragment = LogFragment.newInstance();
        fragmentMap.put(LogFragment.NAME,logFragment);

        SettingFragment settingFragment = SettingFragment.newInstance();
        fragmentMap.put(SettingFragment.NAME,settingFragment);

        ft.add(R.id.fragment,homeFragment)
                .add(R.id.fragment,logFragment)
                .add(R.id.fragment,settingFragment);
        ft.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true; // 表示事件已被处理
        }
        return super.onKeyDown(keyCode, event); // 对于其他按键，调用父类的方法
    }


    @Override
    protected void onDestroy() {
        compositeDisposable.clear();
        Intent intent = new Intent(this, FrpService.class);
        boolean stop = stopService(intent);
        Toast.makeText(this, "已停止服务："+stop, Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
}